﻿using Microsoft.EntityFrameworkCore;
using MiniErp.Entity.DataStreams.DOs.AssetManagement;
using MiniErp.Mysql.Data;
using MiniErp.Repository.BaseRepository;
using MiniErp.Repository.MysqlRepository.IRepository.AssetManagement;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MiniErp.Repository.MysqlRepository.Repository.AssetManagement
{
    /// <summary>
    /// 资产信息增删改查仓储
    /// </summary>
    public class AssetInfoRepository : BaseRepository<MysqlContext, AssetInfo>, IAssetInfoRepository
    {
        public AssetInfoRepository(MysqlContext mysqlContext) : base(mysqlContext) { }
      
        public async Task<List<AssetInfo>> GetFuzzyAssetInfoAsync(string value, int Page, int num)
        {
            var result = await _set
                .Include(item => item.UserInfo)
                .Include(item => item.AssetState)
                .Include(item => item.Department)
                .Include(item => item.Position)
                .Where(item => item.AllId.Contains(value)
                               || item.AssetName.Contains(value)
                               || item.Remarks.Contains(value)
                               || item.Position.PositionName.Contains(value)
                               || item.GeneralType.GeneralTypeName.Contains(value)
                               || item.SecondaryType.SecondaryTypeName.Contains(value)
                               || item.AssetState.AssetStateName.Contains(value)
                               || item.Department.DepartmentName.Contains(value)
                               || item.UserInfo.UserName.Contains(value))
                 .OrderBy(item => item.AllId)
                 .ThenBy(item => item.GeneralTypeId)
                 .ThenBy(item => item.SecondaryTypeId)
                 .ThenBy(item => item.AId)
                 .Skip((Page - 1) * num)
                 .Take(num)
                 .ToListAsync();
            return result;
        }
        public async Task<int?> GetMaxAssetInfoAIdAsync(int gid, int sid)
        {
            return await _set
                .Where(item => item.GeneralTypeId == gid && item.SecondaryTypeId == sid).Select(item => item.AId).MaxAsync();
        }



    }
}
